package com.yhy.wxzn.wcs.websocket.bean;

import com.yhy.wxzn.wcs.SystemInit;
import com.yhy.wxzn.wcs.device.rgv.bean.Coord;
import com.yhy.wxzn.wcs.device.rgv.bean.FourWayRgv;
import com.yhy.wxzn.wcs.device.rgv.bean.Node;
import com.yhy.wxzn.wcs.enums.TaskStatus;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Slf4j
@Data
public class RgvInfo {

    private Integer rgvId;

    private Coord coord;

    private Integer taskId;

    private TaskStatus taskStatus;

    private Integer electricity;



    public static List<RgvInfo> buildRgvInfos(){
        ArrayList<RgvInfo> list = new ArrayList<>();
        for (FourWayRgv rgv : SystemInit.fourWayRgvs) {

            RgvInfo rgvInfo = new RgvInfo();
            rgvInfo.setRgvId(rgv.getDeviceInfo().getId());
            Coord coord = new Coord();
            try {
                coord = rgv.getCurLocation();
            }catch (Exception e){
            }

            rgvInfo.setCoord(coord);
            rgvInfo.setTaskStatus(rgv.getCurTask() == null ? TaskStatus.NULL:rgv.getCurTask().getTaskStatus() == TaskStatus.ERROR ? TaskStatus.ERROR:TaskStatus.ON_GOING);
            rgvInfo.setTaskId(rgv.getCurTask() == null? 0 : rgv.getCurTask().getTaskId());
            rgvInfo.setElectricity(rgv.getFourCarPlc().getDcdl());

            list.add(rgvInfo);
        }
        return list;
    }

    public static List<RgvInfo> buildTestRgvInfos(){
        ArrayList<RgvInfo> res = new ArrayList<>();
        for (FloorInfo.RgvInfo floorRgv : FloorInfo.RgvInfo.RgvDriveInfo.rgvInfos) {
            RgvInfo info = new RgvInfo();
            info.setTaskId(randomTaskId());
            info.setRgvId(floorRgv.getRgvNo());
            info.setCoord(floorRgv.getLocation());
            info.setTaskStatus(TaskStatus.START);
            info.setElectricity(100);
            res.add(info);
        }
        return res;
    }

    private static Integer randomTaskId() {
        Random random = new Random();
        return random.nextInt(99999);
    }
}
